/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.helpers
{
	import flash.display.DisplayObject;
	
	/**
	 * Initiates ENTER_FRAME event. iHelper instance will help us control the event. 
	 * @author puny
	 * @see com.puny.mvc.helpers.IHelper
	 */
	public class FrameHelper
	{
		/**
		 * 
		 * @param owner a DisplayObject where an ENTER_FRAME event happens.
		 * @param before a function to initialize.
		 * @param loop a main function which is invoked in each frame. This function must return a boolean value. If return <code>true</code>, this function will be invoked one more time. If return <code>false</code>, <code>after</code> function will be invoked.
		 * @param after a function to close.
		 * @param immediately if <code>true</code>, count down starts immediately. Otherwise, invoke <code>IHelper.start()</code> to start counting down.
		 * @return a helper instance.
		 * 
		 */
		public static function setup(owner:DisplayObject,
										 before:Function,
										 loop:Function,
										 after:Function,
										 immediately:Boolean=true):IHelper {
			return ContextHelper.getInstance(EnterFrameContext.getInstance(owner, before, loop, after), immediately);
		}
		
		/**
		 * 
		 * @param owner a DisplayObject where an ENTER_FRAME event happens.
		 * @param handler a class which implements IFrameHandler interface.
		 * @param immediately if <code>true</code>, count down starts immediately. Otherwise, invoke <code>IHelper.start()</code> to start counting down.
		 * 
		 */
		public static function setupWithHandler(owner:DisplayObject, handler:IFrameHandler, immediately:Boolean = true):IHelper {
			return ContextHelper.getInstance(EnterFrameContext.getInstance(owner, handler.before, handler.loop, handler.after), immediately);
		}
		
		/**
		 * Report running frame helpers on console.
		 * 
		 */
		public static function debug():void {
			EnterFrameContext.debug();
		}
		
		public static function cleanup():void {
			EnterFrameContext.clear();
		}
	}
}